<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let url = 'http://www.domain.com/?user=anonymous&id=123&id=456&city=%E5%8C%97%E4%BA%AC&enabled';
        parseParam(url)
        console.log(parseParam(url))
        /* 结果
        { user: 'anonymous',
        id: [ 123, 456 ], // 重复出现的 key 要组装成数组，能被转成数字的就转成数字类型
        city: '北京', // 中⽂需解码
        enabled: true, // 未指定值得 key 约定为 true
        }
*/
        function parseParam(url){
            const paramsStr =/.+\?(.+)$/.exec(url)[1];//将？后面的字符串取出来
            const paramsArr = paramsStr.split('&');
            let paramsObj = {};
            paramsArr.forEach(param =>{
                if(/=/.test(param)){
                    let [key,val] = param.split('=');
                    val = decodeURIComponent(val);//解码
                    val = /^d+$/.test(val) ? parseFloat(val):val;
                    if(paramsObj.hasOwnProperty(key)){
                        paramsObj[key] = [].concat(paramsObj[key], val);
                    }else{
                        paramsObj[key] = val; 
                    }
                }else{
                    paramsObj[param] = true;
                }
            })
            return paramsObj
        }
    </script>
</body>
</html>